package com.sky.controller.admin;

import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 分类管理 Controller
 **/
@Slf4j
@Api(tags = "分类管理接口")
@RestController
@RequestMapping("/admin/category")
public class CategoryController {

    @Resource
    private CategoryService categoryService;

    /**
     * @description: 新增分类
     * @author: tanbobin
     * @date: 2024/5/29 17:59
     * @param:
     * @param categoryDTO
     * @return:
     * @return com.sky.result.Result
     **/
    @ApiOperation("新增分类")
    @PostMapping
    public Result save(@RequestBody CategoryDTO categoryDTO){
        log.info("新增分类,{}", categoryDTO);
        categoryService.save(categoryDTO);
        return Result.success();
    }

    /**
     * @description: 分类分页查询
     * @author: tanbobin
     * @date: 2024/5/29 17:59
     * @param:
     * @param pageQueryDTO
     * @return:
     * @return com.sky.result.Result<com.sky.result.PageResult>
     **/
    @ApiOperation("分类分页查询")
    @GetMapping("/page")
    public Result<PageResult> page(CategoryPageQueryDTO pageQueryDTO){
        log.info("条件分类分页查询，{}",pageQueryDTO);
        PageResult pageResult = categoryService.page(pageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * @description: 根据id删除分类
     * @author: tanbobin
     * @date: 2024/6/1 15:39
     * @param:
     * @param id
     * @return:
     * @return com.sky.result.Result
     **/
    @ApiOperation("根据id删除分类")
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Long id){
        log.info("根据id删除分类,{}", id);
        categoryService.deleteById(id);
        return Result.success();
    }

    /**
     * @description: 根据id查询数据
     * @author: tanbobin
     * @date: 2024/6/1 15:43
     * @param:
     * @param id
     * @return:
     * @return com.sky.result.Result<com.sky.entity.Category>
     **/
    @ApiOperation("根据分类id查询数据")
    @GetMapping("/{id}")
    public Result<Category> queryCategoryById(@PathVariable Long id){
        log.info("根据id = {} 查询数据", id);
        Category category = categoryService.queryById(id);
        return Result.success(category);
    }

    /**
     * @description: 修改分类
     * @author: tanbobin
     * @date: 2024/6/3 10:12
     * @param:
     * @param categoryDTO
     * @return:
     * @return com.sky.result.Result
     **/
    @ApiOperation("修改分类")
    @PutMapping
    public Result update(@RequestBody CategoryDTO categoryDTO){
        log.info("修改分类, {}", categoryDTO);
        categoryService.update(categoryDTO);
        return Result.success();
    }

    /**
     * @description: 禁用/启用分类
     * @author: tanbobin
     * @date: 2024/6/3 10:14
     * @param:
     * @return:
     * @return com.sky.result.Result
     **/
    @ApiOperation("禁用/启用分类")
    @PutMapping("/status/{status}/{id}")
    public Result disableEnable(@PathVariable Integer status, @PathVariable Long id){
        log.info("禁用/启用分类 , {},{}", status, id);
        categoryService.disableOrEnable(status,id);
        return Result.success();
    }

    /**
     * @description: 根据类型查询分类
     * @author: tanbobin
     * @date: 2024/6/6 9:19
     * @param:
     * @param type
     * @return:
     * @return com.sky.result.Result<java.util.List<com.sky.entity.Category>>
     **/
    @ApiOperation("根据类型查询分类")
    @GetMapping("/list")
    public Result<List<Category>> list(Integer type){
        List<Category> categoryList = categoryService.list(type);
        return Result.success(categoryList);
    }
}
